//
//  EMPasswordModViewController.m
//  ElectronicMall
//
//  Created by 史伟文 on 14/12/25.
//  Copyright (c) 2014年 cczu. All rights reserved.
//

#import "EMPasswordModViewController.h"
#import "EMInputTextField.h"
#import "UINavigationController+EMNavi.h"
#import "AppDelegate.h"
#import "MBProgressHUD.h"
#import "EMResetPasswordReqBody.h"
#import "EMResetPasswordRespBody.h"
#import "UserInfo.h"

@interface EMPasswordModViewController ()
{
    EMInputTextField *oldPasswordTF;
    EMInputTextField *password1TF;
    EMInputTextField *password2TF;
    EMInputTextField *captchaTF;
    AppDelegate *app;
}
@end

@implementation EMPasswordModViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.view.backgroundColor = RGB(242, 242, 242);
    app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [self.navigationController setNavigationMode:EMNavigationModeDefault andSubMode:0];
    self.navigationItem.title = @"修改密码";
    
    UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(0, 0, 30, 32);
    [button setImage:[UIImage imageNamed:@"button_导航_返回_白"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc]initWithCustomView:button];
}
- (void)backAction: (UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    scrollView.contentSize = CGSizeMake(320, 800);
    
    int height = 25;
    NSMutableAttributedString *tipString = [[NSMutableAttributedString alloc] initWithString:@"带*的不能为空，请保持手机畅通！"];
    [tipString addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0, 1)];
    [tipString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(1, 1)];
    [tipString addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(2, tipString.length - 2)];
    [tipString addAttribute:NSFontAttributeName value:[UIFont fontWithName:HEITIMEDIUM size:14] range:NSMakeRange(0, tipString.length)];
    UILabel *tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, height, 300, 15)];
    tipLabel.attributedText = tipString;
    [scrollView addSubview:tipLabel];
    
    height += 20 + 10;
    NSMutableAttributedString *oldPassString = [[NSMutableAttributedString alloc] initWithString:@"原密码*"];
    [oldPassString addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0, 3)];
    [oldPassString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(3, 1)];
    [oldPassString addAttribute:NSFontAttributeName value:[UIFont fontWithName:HEITIMEDIUM size:14] range:NSMakeRange(0, oldPassString.length)];
    UILabel *oldPasswordTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(W(10), H(height), 120, 15)];
    oldPasswordTitleLabel.attributedText = oldPassString;
    [scrollView addSubview:oldPasswordTitleLabel];
    height += 15 + 5;
    oldPasswordTF = [[EMInputTextField alloc] initWithFrame:CGRectMake(W(20), H(height), W(290), H(35))];
    [oldPasswordTF setTextFieldMode:EMInputTextFieldModeText];
    oldPasswordTF.inputTextField.delegate = self;
    [scrollView addSubview:oldPasswordTF];
    
    height += 35 + 10;
    NSMutableAttributedString *newPassString = [[NSMutableAttributedString alloc] initWithString:@"新密码*"];
    [newPassString addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0, 3)];
    [newPassString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(3, 1)];
    [newPassString addAttribute:NSFontAttributeName value:[UIFont fontWithName:HEITIMEDIUM size:14] range:NSMakeRange(0, newPassString.length)];
    UILabel *password1TitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(W(10), H(height), 120, 15)];
    password1TitleLabel.attributedText = newPassString;
    [scrollView addSubview:password1TitleLabel];
    height += 15 + 5;
    password1TF = [[EMInputTextField alloc] initWithFrame:CGRectMake(W(20), H(height), W(290), H(35))];
    [password1TF setTextFieldMode:EMInputTextFieldModeText];
    password1TF.inputTextField.delegate = self;
    [scrollView addSubview:password1TF];
    
    height += 35 + 10;
    NSMutableAttributedString *new2PassString = [[NSMutableAttributedString alloc] initWithString:@"重输新密码*"];
    [new2PassString addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0, 5)];
    [new2PassString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(5, 1)];
    [new2PassString addAttribute:NSFontAttributeName value:[UIFont fontWithName:HEITIMEDIUM size:14] range:NSMakeRange(0, new2PassString.length)];
    UILabel *password2TitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(W(10), H(height), 120, 15)];
    password2TitleLabel.attributedText = new2PassString;
    [scrollView addSubview:password2TitleLabel];
    height += 15 + 5;
    password2TF = [[EMInputTextField alloc] initWithFrame:CGRectMake(W(20), H(height), W(290), H(35))];
    [password2TF setTextFieldMode:EMInputTextFieldModeText];
    password2TF.inputTextField.delegate = self;
    [scrollView addSubview:password2TF];
    
    height += 35 + 10;
    NSMutableAttributedString *captchaString = [[NSMutableAttributedString alloc] initWithString:@"手机验证码*"];
    [captchaString addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0, 5)];
    [captchaString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(5, 1)];
    [captchaString addAttribute:NSFontAttributeName value:[UIFont fontWithName:HEITIMEDIUM size:14] range:NSMakeRange(0, captchaString.length)];
    UILabel *captchaTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(W(10), H(height), 130, 15)];
    captchaTitleLabel.attributedText = captchaString;
    [scrollView addSubview:captchaTitleLabel];
    height += 15 + 5;
    UIButton *getCaptchaBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    getCaptchaBtn.frame = CGRectMake(W(180), H(height), W(130), H(35));
    getCaptchaBtn.backgroundColor = RGB(54, 126, 255);
    getCaptchaBtn.layer.cornerRadius = 5;
    getCaptchaBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    [getCaptchaBtn setTitle:@"点击获取验证码" forState:UIControlStateNormal];
    [getCaptchaBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [getCaptchaBtn addTarget:self action:@selector(getCaptchaAction:) forControlEvents:UIControlEventTouchUpInside];
    [scrollView addSubview:getCaptchaBtn];
    captchaTF = [[EMInputTextField alloc] initWithFrame:CGRectMake(W(20), H(height), W(150), H(35))];
    [captchaTF setTextFieldMode:EMInputTextFieldModeText];
    captchaTF.inputTextField.delegate = self;
    [scrollView addSubview:captchaTF];
    
    
    
    height += 30 + 30;
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(W(10), H(height), W(300), H(40));
    btn.backgroundColor = RGB(112, 179, 81);
    btn.layer.cornerRadius = 5;
    btn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [btn setTitle:@"确认修改" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(correctAction:) forControlEvents:UIControlEventTouchUpInside];
    [scrollView addSubview:btn];
    
    [self.view addSubview:scrollView];
    
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBackground:)];
    [scrollView addGestureRecognizer:tapGR];
    
}
#pragma mark - 关闭键盘
- (void)tapBackground:(UIGestureRecognizer *)recog
{
    [self closeKeyboard];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self closeKeyboard];
    return NO;
}

- (void)closeKeyboard
{
    [oldPasswordTF.inputTextField resignFirstResponder];
    [captchaTF.inputTextField resignFirstResponder];
    [password1TF.inputTextField resignFirstResponder];
    [password2TF.inputTextField resignFirstResponder];
}

#pragma mark - 点击事件
- (void)correctAction:(UIButton *)sender
{
    if (oldPasswordTF.inputTextField.text.length == 0) {
        [app showTip:@"请输入原密码"];
        return ;
    }
    if (password1TF.inputTextField.text.length == 0) {
        [app showTip:@"请输入新密码"];
        return ;
    }
    if (![password1TF.inputTextField.text isEqualToString: password2TF.inputTextField.text]) {
        [app showTip:@"两次密码不一致"];
        return ;
    }
    if (captchaTF.inputTextField.text.length == 0) {
        [app showTip:@"请输入验证码"];
        return ;
    }
    
    [MBProgressHUD  showHUDAddedTo:self.navigationController.view animated:YES];
    EMResetPasswordReqBody *reqBody = [[EMResetPasswordReqBody alloc] init];
    reqBody.JUDGEMETHOD = @"MODIFY-PASSWORD";
    reqBody.UID = [UserInfo sharedUserInfo].m_userName;
    reqBody.SESSION = [UserInfo sharedUserInfo].m_session;
    reqBody.OLDPASSWORD = oldPasswordTF.inputTextField.text;
    reqBody.NEWPASSWORD = password1TF.inputTextField.text;
    reqBody.YZM = @"8888";
    
    HessianFormDataRequest *request = [[HessianFormDataRequest alloc] init];
    request.postData = reqBody.keyValues;
    [request startRequestWithURL:baseURL andCompletionBlcok:^(NSDictionary *respInfo) {
        EMResetPasswordRespBody *respBody = [EMResetPasswordRespBody objectWithKeyValues:respInfo];
        [MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
        NSLog(@"%@", respInfo);
        if ([respBody.ERRORCODE isEqualToString: @"0000"]) {
            [app showTip:respBody.SUCCESSFULINFO];
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            [app showTip:respInfo[@"ERRORDESTRIPTION"]];
        }
        
    } andFailedBlock:^{
        
        [app showTip:@"无法连接服务器、请稍后再试"];
    }];

}
- (void)getCaptchaAction:(UIButton *)sender
{
    [app showAlert:@"验证码为8888"];
}

@end
